<?php
defined('IN_LK') or die('Access Denied');
lk::lo_app('admin_common', 'admin', 0);
class admin_log extends admin_common
{
	/**
	 * 数据库句柄
	 */
	private $db;
	
	public function __construct()
	{
		parent::__construct();
		$this->db = lk::load_model('log');
	}
	
	public function initialize()
	{
		$query = $where = $username = $start_time = $end_time = '';
		if(isset($_GET['username']) || (isset($_GET['start_time']) && isset($_GET['end_time'])))
		{
			$_POST['dosubmit'] = 1;
			$_POST['info'] = array(
					'username'   => isset($_GET['username']) ? $_GET['username'] : '',
					'start_time' => $_GET['start_time'],
					'end_time'   => $_GET['end_time']);
		}
		if(isset($_POST['dosubmit']))
		{
			$info = $_POST['info'];
			if($info['username'])
			{
				if(!is_username($info['username']))
				{
					showmsg(LANG('parameters_error'), false);
				}
				$where = '`username`=\'' . $info['username'] . '\'';
				$username = $info['username'];
				$query .= "&username=" . urlencode($username);
			}
			if($info['start_time'] && $info['end_time'])
			{
				if(!is_datetime($info['start_time'], 'time') || !is_datetime($info['end_time'], 'time'))
				{
					showmsg(LANG('parameters_error'), false);
				}
				$where .= $where ? ' AND' : '';
				$where .= " `time`>='" . $info['start_time'] . "' AND `time`<='" . $info['end_time'] . "'";
				$start_time = $info['start_time'];
				$end_time   = $info['end_time'];
				$query .= "&start_time=" . urlencode($start_time) . "&end_time=" . urlencode($end_time);
			}
		}
		$url = '?m=admin&c=admin_log' . $query;
		lk::lo('form');
		$_GET['page'] = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		$logs = $this->db->listinfo('*', $where, $_GET['page'], 15, '`logid` DESC', '', '', $url);
		$page_nav = $this->db->page_nav;
		include self::admin_t('admin_log');
	}
	
	
	/**
	 * 删除日志
	 */
	public function delete()
	{
		$month = isset($_GET['month']) ? intval($_GET['month']) : 0;  //删除一月前数据
		if($month)
		{
			$d_time = TIME - 30 * 24 * 3600;
			$d      = date('Y-m-d H:i:s', $d_time);
			$where  = "`time`<='$d'";
			if($this->db->delete($where))
			{
				exit('1');
			}
			exit('0');
		}
		if(isset($_POST['logid']) && $_POST['logid'])
		{
			$in = '';
			$logid_arr = explode(',', $_POST['logid']);
			foreach($logid_arr as $logid)
			{
				if($logid)
				{
					$in .= '\'' . (int)$logid . '\',';
				}
			}
			$logids = substr($in, 0, -1);
			if($this->db->delete("`logid` IN ($logids)"))
			{
				exit('1');
			}
		}
		exit('0');
	}
}